<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'问诊记录'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$this->worker_id);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yiliao_lesson_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if(!$item['username']){
				$user=pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),array('name','birth','sex','mobile'));
				$item['username']=$user['name'];
				$item['name']=$user['name'];
				$item['mobile']=$user['mobile'];
				$item['sex']=$user['sex'];
				$item['birth']=json_decode($user['birth'],true);
				$item['year']=$item['birth']['year'];
				if($item['bodyid']){
					$body=pdo_get('jy_yiliao_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['bodyid']),array('name','sex','agetime'));
					$item['name']=$body['name'];
					$item['sex']=$body['sex'];
					$item['year']=date('Y',$body['agetime']);
				}
				$item['name']=$item['name']?:'无';
				$item['sex']=$item['sex']==0?'女':'男';
				$item['age']=$item['year']>0?date('Y')-$item['year']:'无';
				pdo_update('jy_yiliao_lesson_log',array('username'=>$item['username'],'name'=>$item['name'],'sex'=>$item['sex'],'age'=>$item['age']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if($item['storeid']){
				$item['storetitle']=pdo_getcolumn('jy_yiliao_store',array('id'=>$item['storeid']),'title');
			}
			$this->model('worker')->upmy_user($item['mid'],$item['bodyid'],$item['worker_id']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'客户信息','type'=>'member'),
			array('title'=>'患者','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'家长：','name'=>'username'),
				array('title'=>'家人：','name'=>'name','unit'=>'sex')
				)
			),
			array('name'=>'age','title'=>'年龄','type'=>'string'),
			array('name'=>'money','title'=>'诊费','type'=>'string'),
			array('name'=>'storetitle','title'=>'所属门诊','type'=>'string'),
			array('name'=>'worker_id','title'=>'大夫','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'order_status'),
		),'action'=>array(
			array('type'=>'post','action'=>'/worker.lesson_log/jiedan/','title'=>'接单','show'=>array('status'=>1)),
			array('type'=>'href','action'=>'/worker.lesson_log/setorder/','title'=>'看诊结束','show'=>array('status'=>2)),
			array('type'=>'href','action'=>'/worker.lesson_log/setorder/','title'=>'修改资料','show'=>array('status'=>3)),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['ispay']=array('未支付','已支付');
	$return['order_status']=$this->order_status;
	exi($return);
}
if($_RQ['op']=='setorder'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array(
			'fan_time'=>strtotime($post['fan_time']),
			'remark'=>trim($post['remark']),
			'baofile'=>ijson_encode($post['baofile']),
			'status'=>3,
		);
		pdo_update('jy_yiliao_lesson_log',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','goback');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yiliao_lesson_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['fan_time']=$item['fan_time']?:time();
		$item['fan_time']=date('Y-m-d H:i',$item['fan_time']);
		if(!$item['username']){
			$user=pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),array('name','birth','sex','mobile'));
			$item['username']=$user['name'];
			$item['name']=$user['name'];
			$item['mobile']=$user['mobile'];
			$item['sex']=$user['sex'];
			$item['birth']=json_decode($user['birth'],true);
			$item['year']=$item['birth']['year'];
			if($item['bodyid']){
				$body=pdo_get('jy_yiliao_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['bodyid']),array('name','sex','agetime'));
				$item['name']=$body['name'];
				$item['sex']=$body['sex'];
				$item['year']=date('Y',$body['agetime']);
			}
			$item['name']=$item['name']?:'无';
			$item['sex']=$item['sex']==0?'女':'男';
			$item['age']=$item['year']>0?date('Y')-$item['year']:'无';
		}else{
			$item['sex']=$item['sex']==0?'女':'男';
		}
	}else{
		exi('请先选择订单','error');
	}
	$item['baofile']=json_decode($item['baofile'],true);
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'提交病例',
	);
	$htmls['edit']=array(
		array('name'=>'username','title'=>'家长','type'=>'copy'),
		array('name'=>'name','title'=>'患者','type'=>'copy'),
		array('name'=>'age','title'=>'年龄','type'=>'copy'),
		array('name'=>'sex','title'=>'性别','type'=>'copy'),
		array('name'=>'fan_time','title'=>'下次复诊时间','type'=>'time'),
		array('name'=>'baofile','title'=>'检查报告','type'=>'images'),
		array('type'=>'text','title'=>'病例详情','name'=>'remark'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='jiedan'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	pdo_update('jy_yiliao_lesson_log',array('status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}